package com.ojoin.ojoin.eb_spider;

import java.awt.Robot;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.ButtonReleaseAction;
import org.openqa.selenium.interactions.ClickAndHoldAction;
import org.openqa.selenium.interactions.MoveMouseAction;
import org.openqa.selenium.interactions.MoveToOffsetAction;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.WaitUtil;

public class AliHumanCAPTCHA {
	
	private Logger log = LoggerFactory.getLogger(AliHumanCAPTCHA.class);
	
	WebDriver driver = null;
	
	@Before
	public void setUp(){
		ProfilesIni allProfiles = new ProfilesIni();
		FirefoxProfile profile = allProfiles.getProfile("default");

		
		// 总是授权
		profile.setPreference("plugin.default.state", 2);
		// 下载
		profile.setPreference("browser.download.folderList", 2);
		profile.setPreference("browser.helperApps.alwaysAsk.force", false);
		profile.setPreference("browser.download.manager.showWhenStarting",
				false);
				
	 
		profile.setPreference("browser.download.dir", "E:\\");
		profile.setPreference(
				"browser.helperApps.neverAsk.saveToDisk",
				"application/vnd.ms-excel,application/octet-stream,text/plain,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
		capabilities.setCapability(FirefoxDriver.PROFILE, profile);
//		String gridHub = "http://123.57.85.143:8880/wd/hub";
		String gridHub = "http://192.168.0.110:8880/wd/hub";
		try {
			driver = new RemoteWebDriver(new URL(gridHub), capabilities);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
	}
	
	
	@After
	public void tearDown(){
		if(null!=driver){
			driver.close();
		}
	}
	
	@Test
	public void slide() throws BusinessException, Exception {

		driver.get("http://member.cainiao.com/login.htm?redirect_url=//work.cainiao.com/");
		
		WebElement iframe =driver.findElement(By.cssSelector(
				"body > div.content > div.wrap-login > iframe"));
		
		driver.switchTo().frame(iframe);

		boolean isLoginIn=false;
		
		WebElement OtherAccountV =null;
		
		try{
			OtherAccountV= driver.findElement(By.id("J_OtherAccountV"));
			isLoginIn=true;
		}catch(Exception e){
			
		}
		
		
		if(isLoginIn){
			OtherAccountV.click();
			
			WebDriverWait wait = new WebDriverWait(driver, 5000);
			wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("TPL_username_1")));
			
		}
		
		
		
		WebElement username = driver.findElement(By.id("TPL_username_1"));
		WebElement pwd = driver.findElement(By.id("TPL_password_1"));
		WebElement sb = driver.findElement(By.id("J_SubmitStatic"));
		
		username.clear();
		username.sendKeys("美的恒通万佳专卖店");
		pwd.sendKeys("meidiHTWJ2016");

		
		WebElement sliderBg = driver.findElement(By.id("nc_1__scale_text"));
		
		WebElement slider = driver.findElement(By.id("nc_1_n1z"));

		Dimension sliderDim=slider.getSize();
		Dimension bgDim=sliderBg.getSize();
		int dist=bgDim.getWidth()-sliderDim.getWidth();
		
		
		Point coordinates = slider.getLocation();
		Robot robot = new Robot();
		robot.mouseMove(coordinates.x,coordinates.y+80);
		
		
        Actions action = new Actions(driver){
        	public Actions dragAndDropBy(WebElement source, int xOffset, int yOffset)
        	  {
        	    this.action.addAction(new ClickAndHoldAction(this.mouse, (Locatable)source));
        	    
        	    int groups[]={1,2,2,5};
        		int stepsX[] = {0,0,0,0};	
        		int stepsY[] = {10,15,19,9};
        		
        	    for(int c=0;c<groups.length;c++){
        			stepsX[c]=groups[c]*xOffset/10;
        			
        			this.action.addAction(new MoveToOffsetAction(this.mouse, null, stepsX[c], stepsY[c]));
                    
        		}
        	    
        	    this.action.addAction(new ButtonReleaseAction(this.mouse, null));
        	    return this;
        	  }
        };
        
		
        action.dragAndDropBy(slider, dist,0).build().perform();
        
        sb.click();
        
		Thread.sleep(5000);
		
		boolean isLoginSuccess = false;
		isLoginSuccess = (new WebDriverWait(driver, 10)).until(
				new ExpectedCondition<Boolean>() {
					public Boolean apply(WebDriver d) {
						return d.getCurrentUrl()
								.equals("http://work.cainiao.com/");
					}
				}).booleanValue();
	 
	
	if(isLoginSuccess){
		log.info("ret :"+isLoginSuccess);
	}
	
	
	}
	
}
